<?php
defined('BASEPATH') OR exit('No direct script access allowed');
set_time_limit(0);

class Index extends CI_Controller {

    protected $data;

    public $products;
    public function __construct()
    {
        // Call the CI_Model constructor
        parent::__construct();
        $this->data['base_url'] = $this->config->config['base_url'];
        $this->products = array(
            '1' =>array('product'=>'天气丹华泫水乳6件套','price'=>'1300'),
            '2' =>array('product'=>'拱辰享水妍水乳4件套','price'=>'720'),
            '3' =>array('product'=>'津率享红华凝香6件套','price'=>'900'),
            '4' =>array('product'=>'秘贴自生三合一套装','price'=>'950'),
            '5' =>array('product'=>'拱辰享气韵生6件套','price'=>'960'),
            '6' =>array('product'=>'拱辰享雪玉凝2件套','price'=>'800')
        );
        $this->load->model('event');
    }


	public function index()
	{
        $this->load->view('pc/index',$this->data);
	}

    public function save()
    {
        $re['code'] = 0;
        $re['data'] = 'id不能为空';
        if(!$this->checkmobile($_POST['phone']))
        {
            $re['data'] = '手机号码不对';
            echo json_encode($re);
            exit;
        }
        $data['event'] = $_POST['event'];
        $data['product'] = $_POST['wfproduct'];
        $data['type'] = 9;
//        $data['price'] = 0.01;
        $data['price']  = $_POST['wfprice'];
        $data['phone'] = $_POST['phone'];
        $data['name'] = $_POST['name'];
        $data['address'] = $_POST['wfprovince'].$_POST['wfcity'].$_POST['wfarea'].$_POST['wfaddress'];
        $data['guest'] = $_POST['wfguest'];
        $data['code'] = $this->getcode();
        $data['time'] = time();
        $event = $this->event->insert($data);
        if(!$event)
        {
            return '发生错误请重试';
        }


        $data['return_url'] = $this->data['base_url']."/index.php/pc/index/pay/{$data['code']}";
        $data['show_url'] =  $this->data['base_url']."/index.php/pc/index/pay/{$data['code']}";
        $this->load->library('Pay');
        $this->pay->zhifu($data);

    }

    public function pay($code)
    {
        if(empty($code))
        {
           echo "未找到该订单";
            return ;
        }
        $event = $this->event->get_events_by(array('code' =>$code));
        $this->load->library('Pay');
        if(empty($event) && !empty($event[0])) {
            echo "未找到该订单";
            return;
        }elseif($event[0]['is_pay'] == 2){
            echo "已支付！";
            return ;
        }elseif($this->pay->return_url())
        {
            echo "支付失败！";
            error_log(date("[Y-m-d H:i:s]")." -[".$_SERVER['REQUEST_URI']."] :".json_encode($_GET)."\n", 3, "pay_err.log");
            return ;
        }

        $data['out_trade_no'] =  $_GET['out_trade_no'];
        $data['trade_no'] =  $_GET['trade_no'];
        $data['trade_status'] =  $_GET['trade_status'];
        if( $_GET['trade_status'] == 'TRADE_SUCCESS') {
            $data['is_pay'] =  2;
        }
        $data['pay_time'] = time();
        $code= $this->event->update_pay($code,$data);
        if($code)
        {
            header('location:'.$this->data['base_url'].'/index.php/pc/index');
        }else{
            echo "更新数据库失败";
        }
    }


    public function checkmobile($mobile)
    {
        if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$/",$mobile)){
            //验证通过
            return true;
        }else{
            //手机号码格式不对
            return false;
        }
    }

    public function getcode()
    {
        return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    }



}
